
const key = 'userToken'
const userNameKey = 'userName'
const userHeadInfoKey = 'userToken'
const state = {
  userToken: "",
  userName:"",
  userHeadInfo:""
};


const getters = {
  getUserToken (state) {
    if (!state.userToken) {
      state.userToken = localStorage.getItem(key);
    }
    return state.userToken;
  },
  getUserName (state) {
    if (!state.userName) {
      state.userName = localStorage.getItem(userNameKey);
    }
    return state.userName;
  },
  getUserHeadInfo (state) {
    if (!state.userHeadInfo) {
      state.userHeadInfo = localStorage.getItem(userHeadInfoKey);
    }
    return state.userHeadInfo;
  }
};

const mutations = {
  change_token (state, userToken) {
    state.userToken = userToken;
    localStorage.setItem(key,userToken);
  },

  change_userName (state, userName) {
    state.userName = userName;
    localStorage.setItem(userNameKey,userName);
  },
  change_userHeadInfo (state, userHeadInfo) {
    state.userHeadInfo = userHeadInfo;
    localStorage.setItem(userHeadInfoKey,userHeadInfo);
  }
};

const actions = {
  changeToken ({commit},userToken) {
    commit("change_token", userToken)
  }
};

// 不要忘记把state, mutations等暴露出去。
export default {
  state,
  mutations,
  actions,
  getters
}

